import Vue from 'vue'
import Router from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import Users from '../components/user/Users.vue'
import Rights from '../components/power/Rights.vue'
import Role from '../components/power/Role.vue'
Vue.use(Router)

const router = new Router({
    routes: [
        { path: '/', redirect: "/login" },
        { path: '/login', component: Login },
        {
            path: '/home',
            component: Home,
            redirect: '/welcome',
            children: [
                { path: '/welcome', component: Welcome },
                { path: '/users', component: Users },
                { path: '/rights', component: Rights },
                { path: '/roles', component: Role },
            ]
        },
    ]
})

//拦截路由导航守卫
router.beforeEach((to, from, next) => {
    //如果用户访问login登录页面 直接放行
    if (to.path === '/login') return next()
        //从 sessionStorage 获取到保存的 token值
    const tokenStr = window.sessionStorage.getItem('token')
        //没有 token则强制跳转到登录页面
    if (!tokenStr) return next('/login')
    next()
})

export default router